//
//  RJHomeVC.m
//  AirQuality
//
//  Created by Po on 15/3/10.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJHomeVC.h"
#import "RJHomeMapVC.h"
#import "RJHomeDetailVC.h"
#import "RJCStationVC.h"

@interface RJHomeVC () <RJMainSuperDelegate>

@property (retain, nonatomic) RJHomeMapVC * mapVC;                      //地图界面
@property (retain, nonatomic) RJHomeDetailVC * detailVC;                //详情界面
@property (retain, nonatomic) UIButton * switchDetailButton;            //切换详情按钮
@property (retain, nonatomic) UIButton * switchMapButton;               //切换地图按钮
@property (retain, nonatomic) UIViewController * currentVC;             //当前视图


- (void)initData;
- (void)initInterface;
@end

@implementation RJHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initInterface];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getHomeMapVC];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
   
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    _mapVC = nil;
    if (_mapVC == nil) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MapViewWillDisappear" object:nil];
    }
}
#pragma mark - user-defined initialization function
- (void)initData
{
    //获得上下两视图的高
    [self setDelegate:self];
}

- (void)initInterface
{
    
    [self.navBar addSubview:[self getSwichDetailButton]];
    self.navBar.isOtherReturnButton = YES;
    if ([RJUserData defaultRJUserData].isProvince) {
        [self.navBar setReturnButtonHidden:NO];
        [self.navBar.returnButton setTitle:@"直管站" forState:UIControlStateNormal];
        self.navBar.returnButton.layer.borderWidth = 2;
        self.navBar.returnButton.layer.borderColor = GetRGBColor(27, 170, 169, 1).CGColor;
        self.navBar.returnButton.layer.cornerRadius = 5;
        CGRect rect = self.navBar.returnButton.frame;
        rect.size.width += 25;
        rect.size.height -= 14;
        rect.origin.y += 3;
//        rect.origin.y = -5;
        [self.navBar.returnButton setFrame:rect];
        [self.navBar.returnButton.titleLabel setTextAlignment:NSTextAlignmentLeft];
        [self.navBar.returnButton removeTarget:nil action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
        [self.navBar.returnButton addTarget:self action:@selector(pressCStationButton:) forControlEvents:UIControlEventTouchUpInside];
    }

}

- (RJHomeMapVC *)getHomeMapVC
{
    if (!_currentVC) {
        _currentVC = [[RJHomeMapVC alloc] init];
        [self addChildViewController:_currentVC];
        [self.view addSubview:_currentVC.view];
//        _currentVC = _mapVC;
    }
    return (RJHomeMapVC *)_currentVC;
}

- (RJHomeDetailVC *)getHomeDetailVC
{
    if (!_currentVC) {
        _currentVC = [[RJHomeDetailVC alloc] init];
        [self addChildViewController:_currentVC];
        [self.view addSubview:_currentVC.view];
//        _currentVC = _detailVC;
    }
    return (RJHomeDetailVC *)_currentVC;
}

#pragma mark - event


- (void)pressCStationButton:(UIButton *)sender
{
    RJCStationVC * vc = [[RJCStationVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)pressDetailButton:(UIButton *)sender
{
    
    [self changePage:1 button:sender];
}

- (void)pressMapButton:(UIButton *)sender
{
    [self changePage:0 button:sender];
}

#pragma mark - function

/**切换页面*/
- (void)changePage:(NSUInteger)page button:(UIButton *)button
{
    [button removeFromSuperview];
    [_currentVC removeFromParentViewController];
    [_currentVC.view removeFromSuperview];
    _currentVC = nil;
    if (page == 0) {
        [self addChildViewController:[self getHomeMapVC]];
        [self.view addSubview:[self getHomeMapVC].view];
        _currentVC = [self getHomeMapVC];
        
        [[self getSwichMapButton] removeFromSuperview];
        [self.navBar addSubview:[self getSwichDetailButton]];

    } else {
        [self addChildViewController:[self getHomeDetailVC]];
        [self.view addSubview:[self getHomeDetailVC].view];
        _currentVC = [self getHomeDetailVC];
        
        [[self getSwichDetailButton] removeFromSuperview];
        [self.navBar addSubview:[self getSwichMapButton]];
    }
}

- (void)reloadHomeView
{
    [_mapVC reloadView];
}
#pragma mark - RJMainSuperDelegate
- (void)RJMainSuperVCFreeAll
{
    [_currentVC removeFromParentViewController];
    [_mapVC removeFromParentViewController];
    [_detailVC.view removeFromSuperview];
    [_mapVC.view removeFromSuperview];
    [_detailVC freeData];
}

#pragma mark - getter
- (UIButton *)getSwichDetailButton
{
    if (!_switchDetailButton) {
        _switchDetailButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 75, 11, 63, 30)];
        [_switchDetailButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _switchDetailButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_switchDetailButton.titleLabel setTextAlignment:NSTextAlignmentLeft];
        [_switchDetailButton setTitle:@"统计" forState:UIControlStateNormal];
        _switchDetailButton.layer.borderWidth = 2;
        _switchDetailButton.layer.borderColor = GetRGBColor(27, 170, 169, 1).CGColor;
        _switchDetailButton.layer.cornerRadius = 5;
        [_switchDetailButton addTarget:self action:@selector(pressDetailButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchDetailButton;
}

- (UIButton *)getSwichMapButton
{
    if (!_switchMapButton) {
        _switchMapButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 75, 11, 63, 30)];
        [_switchMapButton setTitle:@"地图" forState:UIControlStateNormal];
        [_switchMapButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
         _switchMapButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_switchMapButton setImageEdgeInsets:UIEdgeInsetsMake(15, 15, 15, 45)];
        _switchMapButton.layer.borderWidth = 2;
        _switchMapButton.layer.borderColor = GetRGBColor(27, 170, 169, 1).CGColor;
        _switchMapButton.layer.cornerRadius = 5;
        [_switchMapButton addTarget:self action:@selector(pressMapButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchMapButton;
}

@end
